/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;
import model.query;
import model.login;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.student;
import model.doctor;


@WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
public class Servlet extends HttpServlet {

    private static String Login = "/login.jsp";
    private static String Student = "/Student.jsp";
    private static String Doctor = "/Doctor.jsp";
    private static String Addgra = "/Addgrades.jsp";
    private static String Edit = "/Editdate.jsp";
    private static String update = "/confirmupdate.jsp";
     private static String AddDrop = "/AddDrop courses.jsp";
     private static String finall = "/final.jsp";
     private static String editdate = "/Editdate.jsp";
     private static String doctorregister = "/doctorregister.jsp";
     private static String choosetoregister="/choosetoregister.jsp";
     private static String studentregister="/studentregister.jsp";
     private static String finaldoctorpage="/finaldoctorpage.jsp";
    private query que;
    public Servlet() {
       super();
       que=new query();
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          String redirect = "";
        String email = request.getParameter("emailAddress");
        String pass = request.getParameter("password");
        
        String action = request.getParameter("action");
       System.out.println("da5al fel servlet");
        if ( action.equalsIgnoreCase("login")){
            System.out.println("da5al fel login");
        login log= new login();
        log.setPwd(pass);
        
        log.setU_mail(email);
        login result;
        String student ="student";
             String doctor="doctor";
             String admin="admin";
        result=que.login(log);
        if (result == null)
            redirect =  Login;
        else{
             
        if(((result.getU_type()).equals(student))){
            student s=que.getstudentinfo(email);
            HttpSession session = request.getSession();
            session.setAttribute("name", s.getname());
            session.setAttribute("ID", s.getU_id());
            session.setAttribute("u_mail", s.getU_mail());
            session.setAttribute("g1", s.getG1());
            session.setAttribute("g2", s.getG2());
            session.setAttribute("g3", s.getG3());
            session.setAttribute("g4", s.getG4());
            session.setAttribute("g5", s.getG5());
            session.setAttribute("g6", s.getG6());
            session.setAttribute("c1", s.getc1());
            session.setAttribute("c2", s.getc2());
            session.setAttribute("c3", s.getc3());
            session.setAttribute("c4", s.getc4());
            session.setAttribute("c5", s.getc5());
            session.setAttribute("c6", s.getc6());
            
            redirect =Student ;}
        
        
        if(((result.getU_type()).equalsIgnoreCase(doctor))){
            
            doctor d=que.getdoctorinf(email);
            HttpSession session = request.getSession();
            
           session.setAttribute("doctorname", d.getname());
           session.setAttribute("coursename", d.getcourse());
           session.setAttribute("coursedate", d.getdate());
           session.setAttribute("coursetime", d.gettime());
           session.setAttribute("emaildoctor", d.getU_mail());
            redirect =Doctor ;}
        if(((result.getU_type()).equals(admin))){
            redirect =choosetoregister;
        }
       
        
           }
        }
        
        
       
        
        if(action.equalsIgnoreCase("Add Grades")){
           // HttpSession userSession = request.getSession(false);
        //String ename = userSession.getAttribute("emaildoctor").toString();
            //String ename="taha";
           List<String> students=que.getallstudent();
           
        HttpSession session = request.getSession(false);
        session.setAttribute("students", students);
       redirect =Addgra;
        }
        
        if(action.equalsIgnoreCase("Edit Time")){
            doctor c;
            c=new doctor();
            c=que.getdoctorinf(email);
            HttpSession session = request.getSession(false);
           /* session.setAttribute("doctorname2", c.getname());
            session.setAttribute("doctordate2", c.getdate());
            session.setAttribute("doctortime2", c.gettime());
            session.setAttribute("doctorcourse", c.getcourse());*/
          
            
            redirect=editdate;
            
            
            
            
            
            
            
            
            
        }
        if(action.equalsIgnoreCase("confirm submit")){
            
             HttpSession session = request.getSession(false);
           String doctorname =  request.getSession(false).getAttribute("doctorname").toString();
             String editdate = request.getParameter("editdate");
            String edittime = request.getParameter("edittime");
             
            doctor s=new doctor();
            s.setdate(editdate);
            s.settime(edittime);
            s.setname(doctorname);
           String msg=que.editdate(s);
           System.out.println("da5al fel servlet");
           redirect=finaldoctorpage;
            /*if(msg=="Your Time And Date Updates Successfully"){
                redirect=update;
            }
            else{
            redirect=Edit;
        }*/
            
           
        }
        
        
         if(action.equalsIgnoreCase("return")){
           redirect=Edit;
         }
        if(action.equalsIgnoreCase("Add/Drop")){ 
        List<String> courses=que.getallcourses();
        HttpSession session = request.getSession(false);
        session.setAttribute("courses", courses);
        redirect=AddDrop;
        
        //String[] rcourses=request.getParameterValues("courses1");
        //que.registercourses(rcourses);
        }
        
        if(action.equalsIgnoreCase("realAddDrop")){
            
            
            
            
            
            
        }
            
        if(action.equalsIgnoreCase("sumbit")){
            List<String> databaseCourses = que.getallcourses();
            List<String> selectedCourses = new ArrayList<String>();
            for(int i = 0 ; i<databaseCourses.size();i++){
                
                if( request.getParameter(databaseCourses.get(i)) != null)
                    selectedCourses.add(databaseCourses.get(i));
            }
               que.save(selectedCourses,request.getSession(false).getAttribute("u_mail").toString());
               redirect=finaldoctorpage;
        }
        if(action.equalsIgnoreCase("realaddgrade")){
            List<String> databaseStudents = que.getallstudents();
            List<String> selectedStudents = new ArrayList<String>();
            List<String> selectedGrades = new ArrayList<String>();
            for(int i = 0 ; i<databaseStudents.size();i++){
                
                if( request.getParameter(databaseStudents.get(i)) != null)
                    selectedStudents.add(databaseStudents.get(i));
                    selectedGrades.add(request.getParameter(databaseStudents.get(i)));
            }
            que.saveStudentsGrades(selectedStudents,selectedGrades);
            redirect=finaldoctorpage;
            
            
            
            
            
        }
        if(action.equalsIgnoreCase("registerdocotr2")){
            String u_mali = request.getParameter("u_mail");
            String u_type = "doctor";
            String pwd = request.getParameter("pwd");
            String d_id = request.getParameter("d_id");
            String name = request.getParameter("name");
            String courses = request.getParameter("courses");
            String date = request.getParameter("date");
            String time = request.getParameter("time");
            login l=new login();
            l.setPwd(pwd);
            l.setU_type(u_type);
            l.setU_mail(u_mali);
            doctor d=new doctor();
            d.setD_id(d_id);
            d.setU_mail(u_mali);
            d.setcourse(courses);
            d.setdate(date);
            d.settime(time);
            d.setname(name);
            que.registerdoctor(l,d);
            
              redirect= choosetoregister ;
            
            
            
            
            
            
        }
        if(action.equalsIgnoreCase("registerdocotr1")){
            redirect= doctorregister;
        }
         if(action.equalsIgnoreCase("registerstudent")){
             redirect=studentregister;
         }
        if(action.equalsIgnoreCase("studentregister2")){
            String u_mali = request.getParameter("u_mail");
            String u_type = "student";
            String pwd = request.getParameter("pwd");
            String u_id = request.getParameter("u_id");
            String name = request.getParameter("name");
            String level = request.getParameter("level");
            
            login l=new login();
            l.setPwd(pwd);
            l.setU_type(u_type);
            l.setU_mail(u_mali);
            student s=new student();
            s.setU_mail(u_mali);
            s.setU_id(u_id);
            s.setname(name);
            s.setlevel(level);
            
       
            
            
            que.registerstudent(l,s);
            
              redirect=choosetoregister ;
            
            
            
            
            
        }
        if(action.equalsIgnoreCase("finaldoctorpage")){
            redirect=Login;
        }
        
        
        
        RequestDispatcher rd = request.getRequestDispatcher(redirect);
        rd.forward(request, response);
         
        
        
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    }